#include <SDL/SDL.h>
#include<stdint.h>
#include<stdlib.h>
#include<stdio.h>
/*Uint32 getpixel(SDL_Surface *surface,int x,int y)
{
    int bpp = surface->format->BytesPerPixel;
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

    default:
        return 0;
    }
}

void GetPixelColor(SDL_Surface * surface,int x,int y,Uint8 *r,Uint8 *g,Uint8 *b)
{
    SDL_GetRGB(getpixel(surface,x,y),surface->format,r,g,b);
}


int selection(SDL_Surface *surface,int x,int y)
{
    Uint8 b=0,r=0,g=0;
    GetPixelColor(surface,x,y,&r,&g,&b);
    if(r==255 && g==0 && b==0)
    {
        return -1;
    }
}*/
int main(int argc, char *argv[])
{
    SDL_Surface *screen;
    SDL_Event event;
    SDL_Surface *menu;
    SDL_Surface *temp;
    SDL_Surface *aide;
    SDL_Surface *option;
    SDL_Surface *credits;
    SDL_Surface *stage1;
    SDL_Surface *couleur;
    SDL_Rect src, dest;

    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("jeu des mots caches", NULL);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    temp = SDL_LoadBMP("img/menu.bmp");
    if (temp == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    couleur = SDL_LoadBMP("img/couleur.bmp");
    if (couleur == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    option = SDL_LoadBMP("img/option.bmp");
    if (option == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    aide = SDL_LoadBMP("img/aide.bmp");
    if (aide == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    credits = SDL_LoadBMP("img/credits.bmp");
    if (credits == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    stage1 = SDL_LoadBMP("img/stage1.bmp");
    if (stage1 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    menu = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    src.x = 0;
    src.y = 0;
    src.h = menu->h;
    src.w = menu->w;
    dest.x=0;
    dest.y=0;
    SDL_BlitSurface(menu, &src, screen, &dest);
    SDL_Flip(screen);
    int done = 0;
    while(!done)
    {
        while (SDL_PollEvent(&event));
        {
            switch(event.type)
            {
            case SDL_QUIT:
                done= 1;
                break;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done=1;
            }
            break;
            case SDL_MOUSEBUTTONUP:
            {
                if ((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>36 &&event.button.x<247) && (event.button.y>205 &&event.button.y<223))
                {
                    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
                    SDL_BlitSurface(couleur, &src, screen, &dest);
                    SDL_BlitSurface(stage1, &src, screen, &dest);
                    //selection(couleur,event.button.x,event.button.y);
                }
                /*ce if me permet d'acceder a une nouvelle partie en tapant
                     sur nouveau jeu au menu principal*/
                if ((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>41 &&event.button.x<108) && (event.button.y>359 &&event.button.y<376))
                {
                    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
                    SDL_BlitSurface(aide, &src, screen, &dest);
                    /*ce if me permet d'acceder a une nouvelle fenetre en tapant
                     sur aide au menu principal*/
                }
                 if ((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>36 &&event.button.x<162) && (event.button.y>397 &&event.button.y<412))
                {
                    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
                    SDL_BlitSurface(credits, &src, screen, &dest);
                }
                /*ce if me permet d'acceder a une nouvelle fenetre en tapant
                     sur credits au menu principal*/
                    if((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>587 &&event.button.x<633) && (event.button.y>8 &&event.button.y<51))
                    {
                        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
                        SDL_BlitSurface(menu, &src, screen, &dest);
                    }
                    if((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>36 &&event.button.x<166) && (event.button.y>322 &&event.button.y<340))
                    {
                        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
                        SDL_BlitSurface(option, &src, screen, &dest);
                    }
                    SDL_Flip(screen);

                if ((event.button.button == SDL_BUTTON_LEFT) && (event.button.x>38 &&event.button.x<163) && (event.button.y>429 &&event.button.y<446))
                    done=1;
                    /*ce if me permet de quitter le jeu en tapant sur quitter*/
            }
            break;
            }
        }

    }
    SDL_FreeSurface(menu);
    SDL_FreeSurface(aide);
    SDL_FreeSurface(credits);
    SDL_FreeSurface(stage1);
    SDL_FreeSurface(option);
    return 0;
}
